package fon.rm2.server.rs232;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

import fon.rm2.server.rs232.DatabaseConnectionClass.HistoryRecord;
import com.qt.datapicker.DatePicker;


public class CommunicationInterface extends JFrame {

	private static final long serialVersionUID = 1L;
	private static CommunicationInterface instance = null;
	
	
	    private javax.swing.JButton jButton1;
	    private javax.swing.JButton jButton2;
	    private javax.swing.JButton jButton3;
	    private javax.swing.JLabel jLabel1;
	    private javax.swing.JLabel jLabel2;
	    private javax.swing.JLabel jLabel3;
	    private javax.swing.JPanel jPanel1;
	    private javax.swing.JScrollPane jScrollPane1;
	    private javax.swing.JTable jTable1;
	    private javax.swing.JTextField jTextField1;
	    ObservingTextField jTextField2=null;
	    ObservingTextField jTextField3=null;
	    private javax.swing.JPanel mainPanel;
	    private javax.swing.JMenuBar menuBar;
	    private javax.swing.JLabel statusAnimationLabel;
	    private javax.swing.JLabel statusMessageLabel;
	    private javax.swing.JPanel statusPanel;

	public CommunicationInterface(){
		
	}
	
	public static CommunicationInterface getInstance()  {
		if (instance==null){
			instance=new CommunicationInterface();
			instance.initialise();
		}
		return instance;
	}
	
	 
	
	private void initialise(){
		setSize(530,450);
		setTitle("Communication Interface");
		setLocationRelativeTo(null);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
      
		
		 mainPanel = new javax.swing.JPanel();
	        jPanel1 = new javax.swing.JPanel();
	        jLabel1 = new javax.swing.JLabel();
	        jLabel2 = new javax.swing.JLabel();
	        jLabel3 = new javax.swing.JLabel();
	        jTextField1 = new javax.swing.JTextField();
	        jTextField2 = new ObservingTextField();
	        jButton1 = new javax.swing.JButton();
	        jTextField3 = new ObservingTextField();	        
	        jScrollPane1 = new javax.swing.JScrollPane();
	        jTable1 = new javax.swing.JTable();
	        jButton2 = new javax.swing.JButton();
	        jButton3 = new javax.swing.JButton();
	        menuBar = new javax.swing.JMenuBar();
	        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
	        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
	        javax.swing.JMenuItem historyMenuItem = new javax.swing.JMenuItem();
	        statusPanel = new javax.swing.JPanel();
	        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
	        statusMessageLabel = new javax.swing.JLabel();
	        statusAnimationLabel = new javax.swing.JLabel();

	        mainPanel.setName("mainPanel"); 
	        mainPanel.setVisible(false);
	        
	        jPanel1.setName("jPanel1"); 

	        jLabel1.setText("Enter user ID> "); 
	        jLabel1.setName("jLabel1");

	        jLabel2.setText("Date FROM>"); 
	        jLabel2.setName("jLabel2"); 

	        jLabel3.setText("Date TO>"); 
	        jLabel3.setName("jLabel3"); 

	       
	        jTextField1.setName("jTextField1");
	       
	        jTextField2.setName("jTextField2");
	        jTextField2.setEditable(false);
	        jButton1.setText("Search"); 
	        jButton1.setName("jButton1"); 


	        jTextField3.setName("jTextField3"); 
	        jTextField3.setEditable(false);

	        jScrollPane1.setName("jScrollPane1");

	        jTable1.setModel(new javax.swing.table.DefaultTableModel(
	            new Object [][] {
	                {null, null, null, null},
	                {null, null, null, null},
	                {null, null, null, null},
	                {null, null, null, null}
	            },
	            new String [] {
	                "USER_ID", "DATE FROM", "TIME", "STATUS"
	            }
	        ));
	        jTable1.setName("jTable1"); 
	        jTable1.setAutoscrolls(true);
	        jScrollPane1.setViewportView(jTable1);
	        final DefaultTableModel model = new DefaultTableModel();
	        jTable1.setModel(model);
	        model.setColumnIdentifiers(new String[] {"USER_ID", "DATE FROM", "TIME","STATUS"});


	        jButton2.setText("chose");
	      

	        jButton3.setText("chose");
	      

	        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
	        jPanel1.setLayout(jPanel1Layout);
	        jPanel1Layout.setHorizontalGroup(
	            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(jPanel1Layout.createSequentialGroup()
	                .addContainerGap()
	                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addGroup(jPanel1Layout.createSequentialGroup()
	                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addGroup(jPanel1Layout.createSequentialGroup()
	                                .addComponent(jLabel1)
	                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
	                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                                .addComponent(jButton1))
	                            .addGroup(jPanel1Layout.createSequentialGroup()
	                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                                    .addGroup(jPanel1Layout.createSequentialGroup()
	                                        .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
	                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                                        .addComponent(jButton2))
	                                    .addComponent(jLabel2))
	                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                                    .addComponent(jLabel3)
	                                    .addGroup(jPanel1Layout.createSequentialGroup()
	                                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
	                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                                        .addComponent(jButton3)))))
	                        .addContainerGap())
	                    .addGroup(jPanel1Layout.createSequentialGroup()
	                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 425, javax.swing.GroupLayout.PREFERRED_SIZE)
	                        .addContainerGap(11, Short.MAX_VALUE))))
	        );
	        jPanel1Layout.setVerticalGroup(
	            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(jPanel1Layout.createSequentialGroup()
	                .addContainerGap()
	                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jLabel1)
	                    .addComponent(jButton1)
	                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                        .addComponent(jLabel3)
	                        .addComponent(jLabel2))
	                    .addGroup(jPanel1Layout.createSequentialGroup()
	                        .addGap(20, 20, 20)
	                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                            .addComponent(jButton3)
	                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                            .addComponent(jButton2)
	                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
	                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addGap(21, 21, 21))
	        );

	        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
	        mainPanel.setLayout(mainPanelLayout);
	        mainPanelLayout.setHorizontalGroup(
	            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(mainPanelLayout.createSequentialGroup()
	                .addContainerGap()
	                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addContainerGap(12, Short.MAX_VALUE))
	        );
	        mainPanelLayout.setVerticalGroup(
	            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(mainPanelLayout.createSequentialGroup()
	                .addContainerGap()
	                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addContainerGap(17, Short.MAX_VALUE))
	        );

	        menuBar.setName("menuBar"); 

	        fileMenu.setText("File");
	        fileMenu.setName("fileMenu"); 

	      
	        exitMenuItem.setText("Exit"); 
	        exitMenuItem.setName("exitMenuItem"); 
	        exitMenuItem.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent e) {
	            	System.exit(0);
	            };
	        });
	        
	        historyMenuItem.setText("Show user history"); 
	        historyMenuItem.setName("historyMenuItem");
	        historyMenuItem.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent e) {
	            	mainPanel.setVisible(true);
	            };
	        });
	        
	        fileMenu.add(historyMenuItem);
	        fileMenu.add(exitMenuItem);

	        menuBar.add(fileMenu);
	        
	        statusPanel.setName("statusPanel"); 

	        statusPanelSeparator.setName("statusPanelSeparator"); 

	        statusMessageLabel.setName("statusMessageLabel"); 
	        
	        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
	        statusAnimationLabel.setName("statusAnimationLabel");

	        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
	        statusPanel.setLayout(statusPanelLayout);
	        statusPanelLayout.setHorizontalGroup(
	            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 468, Short.MAX_VALUE)
	            .addGroup(statusPanelLayout.createSequentialGroup()
	                .addContainerGap()
	                .addComponent(statusMessageLabel)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 448, Short.MAX_VALUE)
	                .addComponent(statusAnimationLabel)
	                .addContainerGap())
	        );
	        statusPanelLayout.setVerticalGroup(
	            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(statusPanelLayout.createSequentialGroup()
	                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(statusMessageLabel)
	                    .addComponent(statusAnimationLabel))
	                .addGap(3, 3, 3))
	        );

	        
	        setJMenuBar(menuBar);

        add(mainPanel);
    
        
        jButton2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // instantiate the DatePicker
                DatePicker dp = new DatePicker( jTextField3, Locale.UK);
                // previously selected date
               Date selectedDate = dp.parseDate(jTextField3.getText());
                dp.setSelectedDate(selectedDate);
                dp.start(jTextField3);
            };
        });
        
        jButton3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // instantiate the DatePicker
                DatePicker dp = new DatePicker( jTextField2, Locale.UK);
                // previously selected date
               Date selectedDate = dp.parseDate(jTextField2.getText());

                dp.setSelectedDate(selectedDate);
                dp.start(jTextField2);
            };
        });
        
        jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
             try {
            	 
            	 if(!jTextField3.getText().equals("")|| !jTextField2.getText().equals("")){
            	 
            		 DateFormat formatter ; 
                     formatter = new SimpleDateFormat("dd/MM/yy");
                     Date date = (Date)formatter.parse(jTextField3.getText()); 
                     Date date2 = (Date)formatter.parse(jTextField2.getText()); 
                	 
                	 if(date2.before(date)){
                		 JOptionPane.showMessageDialog(mainPanel,"Error! Check dates! ");
                		 return;
                	 }
            	 }
                 
            	 
 				DefaultTableModel dm = (DefaultTableModel)jTable1.getModel();
				dm.getDataVector().removeAllElements();
				
            	ArrayList<HistoryRecord> history = DatabaseConnectionClass.readHistory(jTextField1.getText(),jTextField3.getText(),jTextField2.getText());
				
            	if(history.size()==0){
            		 JOptionPane.showMessageDialog(mainPanel,"There are no records for this user in this period.");
            		 return;
            	}
            	
            	for (HistoryRecord p : history)
		        {
		            model.addRow(new String[] {p.getID(), p.getDate(), p.getTime(), p.getStatus()});
		        }
				
				
			} catch (SQLException e1) {
				e1.printStackTrace();
			} catch (ParseException e2) {
				e2.printStackTrace();
			}
            };
        });
		
        
        
	}
	

	@SuppressWarnings("serial")
	class ObservingTextField extends JTextField implements Observer {
	    public void update(Observable o, Object arg) {
	        Calendar calendar = (Calendar) arg;
	        DatePicker dp = (DatePicker) o;
	        setText(dp.formatDate(calendar));
	    }
	}

	


	
}
